package com.ptszyxx.popose.module.main.trip.vm;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.oos.YOosUtil;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.utils.YSPUtils;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;
import com.ysg.widget.pic.SelectPicBean;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TripReleaseVM extends BaseViewModel<CommonRepository> {
    public ObservableField<String> destination = new ObservableField();
    public ObservableField<String> tag = new ObservableField();
    public ObservableField<String> name = new ObservableField();
    public ObservableField<String> setOutTime = new ObservableField();
    public ObservableField<String> reachTime = new ObservableField();
    public ObservableField<String> travel = new ObservableField();
    public ObservableField<String> remark = new ObservableField();

    public TripReleaseVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onNameEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onSetOutTimeEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onReachTimeEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onTravelEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onSubmitEvent = new SingleLiveEvent<>();
    }

    public BindingCommand onReleaseCommand = new BindingCommand(()-> check());
    public BindingCommand onNameCommand = new BindingCommand(()-> uc.onNameEvent.call());
    public BindingCommand onSetOutTimeCommand = new BindingCommand(()-> uc.onSetOutTimeEvent.call());
    public BindingCommand onReachTimeCommand = new BindingCommand(()-> uc.onReachTimeEvent.call());
    public BindingCommand onTravelCommand = new BindingCommand(()-> uc.onTravelEvent.call());


    public void requestOosToken(){
        YOosUtil.getInstance().getOosToken(model, this);
    }

    public void requestUploadPic(List<SelectPicBean> list) {
        showDialog("图片上传中");
        String userId = YSPUtils.getInstance().getUserId();
        YOosUtil.getInstance().upload(list, userId, new YOosUtil.OnOosListener() {
            @Override
            public void onSuccess(String url) {
            }

            @Override
            public void onSuccess(List<String> list) {
                requestTripSave(list);
            }

            @Override
            public void onError() {
            }
        });
    }

    /**
     */
    public void requestTripSave(List<String> list){
        Map<String, String> map = new HashMap<>();
        map.put("destination", destination.get());
        map.put("tag", tag.get());
        map.put("name", name.get());
        map.put("setOutTime", setOutTime.get());
        map.put("reachTime", reachTime.get());
        map.put("travel", travel.get());
        map.put("remark", remark.get());
        map.put("media", YStringUtil.clearEndComma(YStringUtil.getIds(list)));

        HttpUtils.getInstance().data(model.tripSave(map), this, new OnSuccessResult() {
            @Override
            public void onSuccessResult(BaseResponse response) {
                YToastUtil.showShort(R.string.success);
                YActivityUtil.getInstance().close(TripReleaseVM.this);
            }
        });
    }

    private void check() {
        if (YStringUtil.isEmpty(destination.get())){
            YToastUtil.showShort(R.string.trip_release_destination_hint);
            return;
        }
        if (YStringUtil.isEmpty(tag.get())){
            YToastUtil.showShort(R.string.trip_release_tag_hint);
            return;
        }
        if (YStringUtil.isEmpty(name.get())){
            YToastUtil.showShort(R.string.trip_release_name_hint);
            return;
        }
        if (YStringUtil.isEmpty(setOutTime.get())){
            YToastUtil.showShort(R.string.trip_release_setOutTime_hint);
            return;
        }
        if (YStringUtil.isEmpty(reachTime.get())){
            YToastUtil.showShort(R.string.trip_release_reachTime_hint);
            return;
        }
        if (YStringUtil.isEmpty(travel.get())){
            YToastUtil.showShort(R.string.trip_release_travel_hint);
            return;
        }

        uc.onSubmitEvent.call();
    }


}
